<template>
  <div>
    <div v-permission="['base-data:unit:query']">
      <page-wrapper content-full-height fixed-height>
        <!-- 数据列表 -->
        <vxe-grid
          id="Unit"
          ref="grid"
          resizable
          show-overflow
          highlight-hover-row
          keep-source
          row-id="id"
          :proxy-config="proxyConfig"
          :columns="tableColumn"
          :toolbar-config="toolbarConfig"
          :custom-config="{}"
          :pager-config="{}"
          :loading="loading"
          height="auto"
        >
          <template #form>
            <j-border>
              <j-form bordered label-width="80px" @collapse="$refs.grid.refreshColumn()">
                <j-form-item label="单位类型">
                  <a-select v-model:value="searchFormData.unitType" allow-clear>
                    <a-select-option
                      v-for="item in $enums.UNIT_TYPE.values()"
                      :key="item.code"
                      :value="item.code"
                      >{{ item.desc }}</a-select-option
                    >
                  </a-select>
                </j-form-item>
                <j-form-item label="单位名称">
                  <a-input v-model:value="searchFormData.unitName" allow-clear />
                </j-form-item>
              </j-form>
            </j-border>
          </template>
          <!-- 工具栏 -->
          <template #toolbar_buttons>
            <a-space>
              <a-button type="primary" :icon="h(SearchOutlined)" @click="search">查询</a-button>
              <a-button
                v-permission="['base-data:unit:add']"
                type="primary"
                :icon="h(PlusOutlined)"
                @click="$refs.addDialog.openDialog()"
                >新增</a-button
              >
            </a-space>
          </template>

          <template #unit_type_default="{ row }">
            {{ $enums.UNIT_TYPE.getDesc(row.unitType) }}
          </template>

          <!-- 操作 列自定义内容 -->
          <template #action_default="{ row }">
            <a-button
              v-permission="['base-data:unit:query']"
              type="link"
              @click="
                (e) => {
                  id = row.id;
                  $nextTick(() => $refs.viewDialog.openDialog());
                }
              "
              >查看</a-button
            >
            <a-button
              v-permission="['base-data:unit:modify']"
              type="link"
              @click="
                (e) => {
                  id = row.id;
                  $nextTick(() => $refs.updateDialog.openDialog());
                }
              "
              >修改</a-button
            >
          </template>
        </vxe-grid>
      </page-wrapper>
    </div>
    <!-- 新增窗口 -->
    <add ref="addDialog" @confirm="search" />

    <!-- 修改窗口 -->
    <modify :id="id" ref="updateDialog" @confirm="search" />

    <!-- 查看窗口 -->
    <detail :id="id" ref="viewDialog" />
  </div>
</template>

<script>
  import { h, defineComponent } from 'vue';
  import * as api from '@/api/base-data/unit';
  import { PlusOutlined, SearchOutlined } from '@ant-design/icons-vue';
  import Add from '@/views/base-data/unit/add.vue';
  import Modify from '@/views/base-data/unit/modify.vue';
  import Detail from '@/views/base-data/unit/detail.vue';

  export default defineComponent({
    name: 'Unit',
    components: {
      Add,
      Modify,
      Detail,
    },
    setup() {
      return {
        h,
        PlusOutlined,
        SearchOutlined,
      };
    },
    data() {
      return {
        loading: false,
        // 当前行数据
        id: '',
        // 查询列表的查询条件
        searchFormData: {
          unitType: null,
          unitName: '',
        },
        // 工具栏配置
        toolbarConfig: {
          // 自定义左侧工具栏
          slots: {
            buttons: 'toolbar_buttons',
          },
        },
        // 列表数据配置
        tableColumn: [
          { type: 'seq', width: 60 },
          {
            field: 'unitType',
            title: '单位类型',
            width: 120,
            slots: { default: 'unit_type_default' },
          },
          { field: 'unitName', title: '单位名称', minWidth: 150 },
          { field: 'displayNum', title: '显示排序', width: 70 },
          { field: 'createTime', title: '创建时间', width: 170 },
          { field: 'updateTime', title: '修改时间', width: 170 },
          { title: '操作', width: 170, fixed: 'right', slots: { default: 'action_default' } },
        ],
        // 请求接口配置
        proxyConfig: {
          props: {
            // 响应结果列表字段
            result: 'datas',
            // 响应结果总条数字段
            total: 'totalCount',
          },
          ajax: {
            // 查询接口
            query: ({ page, sorts, filters }) => {
              return api.query(this.buildQueryParams(page));
            },
          },
        },
      };
    },
    created() {},
    methods: {
      // 列表发生查询时的事件
      search() {
        this.$refs.grid.commitProxy('reload');
      },
      // 查询前构建查询参数结构
      buildQueryParams(page) {
        return Object.assign(
          {
            pageIndex: page.currentPage,
            pageSize: page.pageSize,
          },
          this.buildSearchFormData(),
        );
      },
      // 查询前构建具体的查询参数
      buildSearchFormData() {
        return Object.assign({}, this.searchFormData);
      },
    },
  });
</script>
<style scoped></style>
